<template>
	<el-form ref="form" style="width:400px;" :rules="rules" :model="data" label-width="80px" label-position="left">
		<el-form-item label="名称" prop="name">
			<el-input v-model="data.name" style="width:180px"></el-input>
		</el-form-item>
		<el-form-item label="排序" prop="sort">
			<el-input v-model.number="data.sort" style="width:180px"></el-input>
		</el-form-item>
		<el-form-item style="margin-top: 30px">
			<el-button style="width:120px" type="primary" @click="OnSubmit">保存</el-button>
			<el-button style="width:120px" @click="Hiden">取消</el-button>
		</el-form-item>
	</el-form>
</template>
<script>
	export default {
		components: {

		},
		mounted() {
			//      this.ajax({
			//          url: 'api/CommonDictionary/GetNullData?dataType=1',
			//          success: function(data) {
			//              this.initData = data;
			//              this.data = JSON.parse(JSON.stringify(this.initData));
			//          }
			//      });
		},
		props: ["btnType", "parentId", "code", 'editData'],
		data() {
			var check = (rule, value, callback) => {
				if(!value) {
					callback()
				} else if(!Number.isInteger(value)) {
					callback(new Error('请输入数值'))
				} else {
					callback()
				}
			}
			return {
				data: {},
				initData: {},
				rules: {
					name: [{
						required: true,
						message: '请输入名称',
						trigger: 'blur'
					}],
					sort: [{
						required: true,
						validator: check,
						type: 'number',
						message: '序号必须为数字值'
					}]
				},
			}
		},
		methods: {
			OnSubmit() {
				this.$refs['form'].validate((valid) => {
					if(valid) {
						let apiAction = this.btnType < 3 ? "addDictionary" : "editDictionary";
						this.data.enable = 1;
						this.data.isDelete = 1;
						this.data.level = 2;
						this.data.hasChild = 0;
						this.data.parentId = this.parentId;
						this.data.code = this.code;
						console.log(this.data)
						this.ajax({
							url: 'tDictionary/' + apiAction,
							type: 'post',
							data: this.data,
							success: function(data) {
								this.$emit('Show', false);
								this.data = JSON.parse(JSON.stringify(this.initData));
								this.$emit("Refresh");
							}
						});
					} else {
						return false;
					}
				})
			},
			Hiden() {
				this.$emit("Show", false);
				this.data = JSON.parse(JSON.stringify(this.initData));
			}
		},
		watch: {
			editData: function(newVal) {
				this.data = newVal;
			}
		}
	}
</script>